﻿//----------------------------------------------------------------------
// <copyright file="MasterDatabase.cs" company="NTT DATA BEEN (China)">
// Copyright (c)2012 NTT DATA BEEN (China) Information Technology Co.,Ltd.
// </copyright>
// <author> lidawei </author>
// <summary>
// システム名称　　：
// モジュールＩＤ　：
// モジュール名称　：
// 機能概要　　　　：MasterDatabase类 
// 改訂履歴　　　　：
// 2012-08-01 新規 lidawei    Been0001   
// </summary>
//---------------------------------------------------------------------- 

namespace Ntt.NttData.Been.Management.SolutionA.Sync.Operator.SourceDAL
{
	using System;
	using System.Collections.Generic;
	using System.Data;
	using System.Data.SqlClient;
	using Common.Config;
	using Common.Data;
	using CommonLibrary.Database.SqlServer;
	using Config;
	using Model;

	public class MasterDatabase : DatabaseOperation
	{
		private const string GETBASEMASERVERSQL = @" SELECT [CaseID],[MaServerID],[UpDatetime]  FROM  [PermissionCaseMaServer]";

		/// <summary>
		/// Prevents a default instance of the <see cref="MasterDatabase"/> class from being created.
		/// </summary>
		private MasterDatabase() : base(ConfigurationManager<SyncOperatorConfig>.GetInstance(ConfigType.XmlConfig).MasterDatabaseConnectionString) { }

		/// <summary>
		/// Gets the entity permisssion case ma server.
		/// </summary>
		/// <returns></returns>
		public static List<EntityPermisssionCaseMaServer> GetEntityPermisssionCaseMaServer()
		{
			List<EntityPermisssionCaseMaServer> permisssionCaseMaServers ;
			try
			{
				using (var source = new MasterDatabase())
				{
					permisssionCaseMaServers = source.SelectPermissionCaseMaServer();
				}
			}
			catch (Exception ex)
			{
				throw ;
			} 

			return permisssionCaseMaServers;
		}

		/// <summary>
		/// Selects the permission case ma server.
		/// </summary>
		/// <returns></returns>
		private List<EntityPermisssionCaseMaServer> SelectPermissionCaseMaServer()
		{

			  List<EntityPermisssionCaseMaServer> permisssionCaseMaServers = null;

			try
			{
					if (sqlConnection != null)
					{


						using (SqlDataReader reader = new SqlHelper().ExecuteReader(sqlConnection, CommandType.Text, GETBASEMASERVERSQL))
						{

							while (reader.Read())
							{

								var item = new EntityPermisssionCaseMaServer
								{
									CaseId  = reader.GetString(0),
									MaServerId = reader.GetString(1),
									UpDatetime = reader.GetDateTime(2)
									 
								};

								if (permisssionCaseMaServers == null)
								{
									permisssionCaseMaServers = new List<EntityPermisssionCaseMaServer>();
								}

								permisssionCaseMaServers.Add(item);
							}

							reader.Close();
						}
					}
			}
			catch (Exception ex)
			{
				throw ;
			}
			return permisssionCaseMaServers;
		}
	}
}
